importScripts("/javascripts/webrpc.js");

var seq = 0;
var database = [];

plural_name = '<%= plural_name %>';
singular_name = '<%= singular_name %>';

new webrpc.Service("<%= plural_name %>", {
	init : function() {
		try {
			// always error.
			//
			// last_update::2010.3.14
			// It seems that web sql database is not allowed in web workers.
			// Code belows are automatically create dabase schema as same as
			// server side database(AR). But Reasons above, these code doesn't
			// work now(;-;)
			var db = openDatabase("wmr", "", "", 1024*1024*5);
			
			var latestVersion = obj.db.version;
			var currentVersion = db.version || 0;
			
			if(currentVersion < latestVersion) {
				var sql = 'create table ' + obj.plural_name + ' ( id integer PRIMARY KEY';
				for( var attribute in obj.db.attributes ) {
					sql += ", "+attribute.name+" "+attribute.type;
				}
				sql += ")";
				
				db.changeVersion(db.version, latestVersion, function(tx) {
					tx.executeSql(sql, []);
				});
			}
			
			return 'ok';
		} catch(e) {

			return e;
		}
	},
	
	find: function(obj) {
		// xhr
		var req = new XMLHttpRequest();
		req.open("GET", "/"+ plural_name +".json", false);

		var recv = req.send(); 


		if(typeof(obj.id) == 'string' && obj.id == 'all') {
		}
		
		if(typeof(obj.id) == 'number' && obj.id in database) {
		}

		if(req.status == 200)
			return eval("("+req.responseText+")");
		else
			return null;
	},
	
	create: function(obj) {
		var id = seq++;
		database[id] = obj;
		//localStorage[id] = obj;
		
		// xhr
		var req = new XMLHttpRequest();
		req.open("POST", "/"+plural_name +".json", false);

		var senddata = "commit=Create";
		for (key in obj) {
			if (senddata != "") 
				senddata += "&";
			
			if (key == 'authenticity_token')
				senddata += key + "=" + encodeURIComponent(obj[key]);
			else
				senddata += singular_name + "[" + key + "]=" + encodeURIComponent(obj[key]);
		}
		//Send the proper header information along with the request
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.send(senddata); 

		if(req.status >= 200 && req.status < 300)
			return req.responseText;
		else
			return req.status;
	},
	
	update: function(obj) {
		var req = new XMLHttpRequest();
		req.open("PUT", "/"+plural_name+"/"+obj.id+".json", false);

		//Send the proper header information along with the request

		var senddata = "commit=Update";
		for (key in obj) {
			if (senddata != "") 
				senddata += "&";
			
			if (key == 'authenticity_token')
				senddata += key + "=" + encodeURIComponent(obj[key]);
			else
				senddata += singular_name + "[" + key + "]=" + encodeURIComponent(obj[key]);
		}
		//Send the proper header information along with the request
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.send(senddata); 

		if(req.status >= 200 && req.status < 300)
			return eval("("+req.responseText+")");
		else
			return null;
	},
	
	destroy: function(obj) {
		var req = new XMLHttpRequest();
		req.open("DELETE", "/"+plural_name+"/"+obj.id+".json", false);

		//Send the proper header information along with the request
		req.setRequestHeader("Connection", "close");

		var senddata = "";
		for (key in obj) {
			if (senddata != "") 
				senddata += "&";
			
			senddata += key + "=" + encodeURIComponent(obj[key]);
		}
		//Send the proper header information along with the request
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.send(senddata); 

		if(req.status >= 200 && req.status < 300)
			return eval("("+req.responseText+")");
		else
			return null;
	}
});
